package wiltec.fileSystem
{
	import flash.desktop.NativeApplication;
	import flash.filesystem.File;
	import flash.system.Capabilities;
	
	import mx.controls.Alert;
	
	/**
	 * FileX was created to add applicationPublicDirectory
	 * which was not included with the file class
	 * <br/><br/>
	 * Due to the fact that File is apart of the Flash SDK and the use of static vars i decided it would be easier just to build a simple class
	 * to support the file class
	 * <br/><br/>
	 * All the methods of this class are static so you do not create a reference to the class. Just import FileX
	 * <br/><br/>
	 * import wiltec.fileSystem.FileX;<br/>
	 * var _file:File = FileX.applicationPublicDirectory().resolvePath('myFile.txt');
	 */
	public class FileX
	{
		/**
		 * a storage directory unique to each installed AIR application
		 * but not unique to each user
		 * <br/><br/>
		 * [Win 7] 	c:\programdata\[appname]\
		 * <br/>
		 * [OSX] 	\Library\Application Support\[appname]\
		 * <br/>
		 * [Win XP] c:\Documents and Settings\All Users\Application Data\[appname]\
		 * <br/><br/>
		 * 
		 * Warning: if a suitable location is not available or if OS is not recognizable applicationStorageDirectory is substituted. In the unlikey event that 
		 * applicationStorageDirectory is not writeable method will return a null reference to File & Depending on suppressError True/False default false shows
		 * a Alert Box that displays a user friendly error
		 */
		public static function applicationPublicDirectory(suppressError:Boolean = false):File
		{
			/* Get the application name from the descriptor */
			var appXml:XML = NativeApplication.nativeApplication.applicationDescriptor;
			var ns:Namespace = appXml.namespace();
			var appName:String = appXml.ns::name[0];
			/* declare a file to return */
			var _file:File;
			switch(Capabilities.manufacturer)
			{
				case "Adobe Windows":
				{
					var root:String = "C"; 			/* Windows Root Only Needed for Windows OS */
					var winRoot:Boolean = false; 	/* has C:\programData\ */
					/* Set %System Root% */
					_file = File.userDirectory;
					root = _file.nativePath.charAt(0);
					
					/* Determine if programdata is supported */
					_file = new File(root+":\\programdata");
					
					if(_file.exists)
					{
						/* Windows Version Supports program data */
						_file = new File(root+':\\programdata\\'+appName+'\\');
					}
					else
					{
						/* Windows Version Doesnt Support programdata */
						_file = new File(root+':\\Documents and Settings\\All Users\\Application Data\\'+appName+'\\');
					}
					break;
				}
				case "Adobe Macintosh":
				{
					//_file = new File("/Applications/Wordpress Security Check/"+fileName);
					_file = new File("/Library/Application Support/"+appName+"/");
					break;
				}
				case "Adobe Linux":
				{
					_file = File.applicationStorageDirectory;
					break;
				}
				default:
				{
					_file = File.applicationStorageDirectory;
					break;
				}
			}
			
			/* Directory should be valid, create it */
			try{
				/* try to create the directory as is */
				_file.createDirectory();
			}catch(err:Error){
				/* Fall back to AppStorageDirectory */
				try{
					_file = File.applicationStorageDirectory;
					_file.createDirectory()
				}catch(er:Error){
					/* Unable to write to a store. Display error and give up! */
					File
					Alert.show("Unable to create a vaild directory to write into!"+'\n'+'\n'+_file.nativePath,"Unable to create file store");
				}						
			}
			
			return _file;
		}
		
		/**
		 * The application's private storage directory. 
		 * <br/><br/>
		 * Each AIR application has a unique, persistent application storage directory, which is created when you first access File.applicationStorageDirectory. This directory is unique to each application and user. This directory is a convenient location to store user-specific or application-specific data.
		 * <br/><br/>
		 * When you uninstall an AIR application, whether the uninstaller deletes the application storage directory and its files depends on the platform.
		 * <br/><br/>
		 * The url property for this object uses the app-storage URL scheme (not the file URL scheme). This means that the url string is specified starting with "app-storage:" (not "file:"). Also, if you create a File object relative to the File.applicationStoreDirectory directory (by using the resolvePath() method), the url of the File object also uses the app-storage URL scheme (as in the example).
		 * <br/><br/>
		 * The applicationStorageDirectory property provides a way to reference the application storage directory that works across platforms. If you set a File object to reference the application storage directory using the nativePath or url property, it will only work on the platform for which that path is valid.
		 * <br/><br/>
		 * Note (Mac OS only): To comply with Mac App Store requirements, the physical location of this directory changed between AIR 3.2 and AIR 3.3:
		 * <br/><br/>
		 * •3.2 namespace and earlier: ~/Library/Preferences/appid/Local Store/
		 * <br/>
		 * •3.3 namespace and later: path/Library/Application Support/appid/Local Store/, where path is either ~/Library/Containers/bundle-id/Data (sandboxed environment) or ~ (when running outside a sandboxed environment)
		 */
		public static function applicationStorageDirectory():File
		{
			return File.applicationStorageDirectory;
		}
		
		/**
		 * The folder containing the application's installed files. 
		 * <br/><br/>
		 * The url property for this object uses the app URL scheme (not the file URL scheme). This means that the url string is specified starting with "app:" (not "file:"). Also, if you create a File object relative to the File.applicationDirectory directory (by using the resolvePath() method), the url property of the File object also uses the app URL scheme. 
		 * <br/><br/>
		 * Note: You cannot write to files or directories that have paths that use the app: URL scheme. Also, you cannot delete or create files or folders that have paths that use the app: URL scheme. Modifying content in the application directory is a bad practice, for security reasons, and is blocked by the operating system on some platforms. If you want to store application-specific data, consider using the application storage directory (File.applicationStorageDirectory). If you want any of the content in the application storage directory to have access to the application-privileged functionality (AIR APIs), you can expose that functionality by using a sandbox bridge.
		 * <br/><br/>
		 * The applicationDirectory property provides a way to reference the application directory that works across platforms. If you set a File object to reference the application directory using the nativePath or url property, it will only work on the platform for which that path is valid.
		 * <br/><br/>
		 * On Android, the nativePath property of a File object pointing to the application directory is an empty string. Use the url property to access application files.
		 */
		public static function applicationDirectory():File
		{
			return File.applicationDirectory;
		}
		
		/**
		 * The user's desktop directory.
		 * <br/><br/>
		 * The desktopDirectory property provides a way to reference the desktop directory that works across platforms. If you set a File object to reference the desktop directory using the nativePath or url property, it will only work on the platform for which that path is valid.
		 * <br/><br/>
		 * If an operating system does not support a desktop directory, a suitable directory in the file system is used instead.
		 * <br/><br/>
		 * AIR for TV devices have no concept of a user's desktop directory. Therefore, the desktopDirectory property references the same directory location as File.userDirectory property. The user directory is unique to the application.
		 */
		public static function desktopDirectory():File
		{
			return File.desktopDirectory;
		}
		
		/**
		 * The user's documents directory. 
		 * <br/><br/>
		 * On Windows, this is the My Documents directory (for example, C:\Documents and Settings\userName\My Documents). On Mac OS, the default location is /Users/userName/Documents. On Linux, the default location is /home/userName/Documents (on an English system), and the property observes the xdg-user-dirs setting.
		 * <br/><br/>
		 * The documentsDirectory property provides a way to reference the documents directory that works across platforms. If you set a File object to reference the documents directory using the nativePath or url property, it will only work on the platform for which that path is valid.
		 * <br/><br/>
		 * If an operating system does not support a documents directory, a suitable directory in the file system is used instead.
		 * <br/><br/>
		 * AIR for TV devices have no concept of a user's documents directory. Therefore, the documentsDirectory property references the same directory location as the File.userDirectory property. The user directory is unique to the application.
		 * 
		 */
		public static function documentsDirectory():File
		{
			return File.documentsDirectory;
		}
		
		/**
		 * The user's directory.
		 * <br/><br/> 
		 * On Windows, this is the parent of the My Documents directory (for example, C:\Documents and Settings\userName). On Mac OS, it is /Users/userName. On Linux, it is /home/userName.
		 * <br/><br/>
		 * The userDirectory property provides a way to reference the user directory that works across platforms. If you set the nativePath or url property of a File object directly, it will only work on the platform for which that path is valid.
		 * <br/><br/>
		 * If an operating system does not support a user directory, a suitable directory in the file system is used instead.
		 * <br/><br/>
		 * On AIR for TV devices, the userDirectory property references a user directory that is unique to the application.
		 */
		public static function userDirectory():File
		{
			return File.userDirectory;
		}
	}
}